package net.gz01.wallet.web.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.util.ZODate;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOIDGenerate;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Org;

public class OrgController extends BaseController {

	public void search(){
		render("org/orgList.html");
	}

	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<Org> pageResult = Org.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void lookup(){
		Page<Org> items = Org.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("org/orgLookup.html");
	}

	public void addPage(){
		render("org/orgAdd.html");
	}

	public void updatePage(){
		Org org = Org.dao.findById(getPara("id"));
		setAttr("org", org);
		render("org/orgUpdate.html");
	}

	@Before(Tx.class)
	public void add(){
		Org org = getModel(Org.class,true);
		org.setCode(ZOIDGenerate.getNextOrgCode());
		org.setSafeKey(ZOIDGenerate.getUUID().toUpperCase());
		org.setCreateTime(ZODate.getDateTime());
		org.setUpdateTime(ZODate.getDateTime());
		org.save();
		renderSuccess();
	}

	public void update(){
		Org org = getModel(Org.class);
		org.setUpdateTime(ZODate.getDateTime());
		org.update();
		renderSuccess();
	}

	public void view(){
		Org org = Org.dao.findById(getPara("id"));
		setAttr("org", org);
		render("org/orgView.html");
	}

	public void delete(){
		Org.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}

}
